//
//  ViewController.m
//  1012Category分类展示03
//
//  Created by Apple on 15/10/12.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZDStrategyController.h"
#import "ZDCollectionHeadView.h"
#import "ZDCategoryCollectionCell.h"
#import "ZDTableHeadView.h"
//#import "ZDJumpViewController.h"
#import "Global1.h"


@interface ZDStrategyController ()

@property (nonatomic, strong) ZDTableHeadView *topHeadView;

@end

@implementation ZDStrategyController

- (instancetype)init
{
    //布局
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(80, 110);
    
    layout.headerReferenceSize = CGSizeMake(ZDScreenSize.width, 50);
    
    return [super initWithCollectionViewLayout:layout];
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    
    //collectionView的cell默认是从(0, 0)开始的，所以要想添加顶部view，y坐标必须小于0
    self.topHeadView.frame = CGRectMake(0, -120, ZDScreenSize.width, 120);
    
    [self.collectionView addSubview:self.topHeadView];
    
    //设置collectionView的contentInset，目的是为了展示顶部view 不会覆盖collection cell，设置layout.sectionInset无效
    self.collectionView.contentInset = UIEdgeInsetsMake(120, 0, 0, 0);
    
    
    //注册
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZDCategoryCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"ZDCategoryCollectionCell"];
    
    [self.collectionView registerNib:[UINib nibWithNibName:@"ZDCollectionHeadView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZDCollectionHeadView"];
    
    
    //将流水布局设置到collectionView的布局属性，否则布局无效
//    self.collectionView.collectionViewLayout = self.layout;
    
    
//    self.automaticallyAdjustsScrollViewInsets = NO;
    
}


//- (void)viewWillAppear:(BOOL)animated
//{
//    [super viewWillAppear:animated];
//    
//    ZDLog(@"viewWillAppear:%@ ----- %@", NSStringFromCGRect(self.collectionView.frame), NSStringFromCGRect(self.topHeadView.frame));
//    
//    ZDLog(@"%@", self.topHeadView.subviews);
//}
//
//
//- (void)viewDidAppear:(BOOL)animated
//{
//    [super viewDidAppear:animated];
//    
//    ZDLog(@"viewDidAppear%@", self.topHeadView.subviews);
//}
//
//
//- (void)viewDidDisappear:(BOOL)animated
//{
//    [super viewDidDisappear:animated];
//    
//    ZDLog(@"viewDidDisappear:%@ ----- %@", NSStringFromCGRect(self.collectionView.frame), NSStringFromCGRect(self.topHeadView.frame));
//    
//    ZDLog(@"%@", self.topHeadView.subviews);
//}


#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return 4;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (section == 1)
    {
        return 10;
    }
    
    return 6;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    ZDCategoryCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ZDCategoryCollectionCell" forIndexPath:indexPath];
    
    return cell;
}


- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ZDCollectionHeadView *headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ZDCollectionHeadView" forIndexPath:indexPath];
    
    return headView;
}

#pragma mark - 代理方法
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    //控制器需要自定义，加载网页....  获取模型数据
    //ZDJumpViewController *jumpVc = [[ZDJumpViewController alloc] init];
    
    //[self.navigationController pushViewController:jumpVc animated:YES];
}


#pragma mark 懒加载
- (ZDTableHeadView *)topHeadView
{
    if (!_topHeadView)
    {
        _topHeadView = [ZDTableHeadView tableHeadView];
        
        __weak typeof(self) weakSelf = self;
        
        _topHeadView.itemClickBlock = ^(UIViewController *vc){
        
            [weakSelf.navigationController pushViewController:vc animated:YES];
        };
    }
    
    return _topHeadView;
}



@end
